<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
>

<!-- 导入公有的header -->
<head th:include="common/head::header"/>

<body class="hold-transition skin-blue sidebar-mini">
    <!-- =============================================== -->
    <!-- Main content -->
    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <!-- /.box-header -->
                <!-- form start -->
                <form id="form_wakeup" onsubmit="return false">
                    <div class="box-body">
                        <!-- Select multiple-->
                        <div class="form-group">
                            <label >主机名</label>
                            <input type="text" class="form-control" name="hostname"
                                    placeholder="">
                        </div>
                        <div class="form-group">
                            <label >MAC地址（冒号隔开/横杠隔开）</label>
                            <input type="text" class="form-control" id="macAdress" name="mac"
                                   placeholder="">
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <button type="button" onclick="saveWakeupForm();" class="btn btn-primary">保存</button>
                        <button type="button" onclick="submitWakeup();" class="btn btn-danger">立即唤醒</button>
                    </div>
                </form>
            </div>
            <!-- /.col -->
        </div>


        <div class="row">
            <div class="col-md-12">
                <!-- /.box-header -->
                <div class="box-body no-padding">
                    <table class="table">
                        <tr>
                            <th style="width: 10px">#</th>
                            <th>主机名</th>
                            <th>MAC地址</th>
                            <th style="width: 80px">操作</th>
                        </tr>
                        <tr th:each="p,rowIndex: ${list}">
                            <td><span class="badge bg-red"  th:text="${rowIndex.index+1}"></span></td>
                            <td th:text="${p.hostName}"></td>
                            <td th:text="${p.mac}">
                            </td>
                            <td>
                                <button type="button"  class="btn btn-danger btn-xs" th:onclick="execDelete([[${p.dataId}]]);"><i class="fa fa-remove" ></i></button>
                                <button type="button"  class="btn btn-success btn-xs" th:onclick="execWakeup([[${p.mac}]]);"><i class="fa fa-play" ></i></button>
                            </td>
                        </tr>
                    </table>
                </div>
            <!-- /.box -->
            </div>
        <!-- /.col -->
        </div>


    </section>
    <!-- /.content -->
    <script type="application/javascript">
        //保存安全配置
        function saveWakeupForm(){
            var params = jQuery('#form_wakeup').serialize();
            var urlStr = ctxPath + '/jm/manager/wakeup/saveWakeUp';
            jQuery.ajax({
                url: urlStr,
                type: "POST",
                dataType: 'json',//接受数据格式
                data: params,   //要传递的数据
                async: false,
                success: function (data) {
                    if(data.data){
                        window.parent.alertSuccess('保存配置成功！');
                        window.location.reload();
                    }else{
                        window.parent.alertError('保存配置失败！');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    window.parent.alertError('保存配置异常！');
                }
            });
        }

        function submitWakeup(){
            var macAdress = $('#macAdress').val();
            execWakeup(macAdress);
        }

        //执行网络唤醒，依据mac
        function execWakeup(macadress){
            var urlStr = ctxPath + '/jm/manager/wakeup/execWakeUp';
            jQuery.ajax({
                url: urlStr,
                type: "POST",
                dataType: 'json',//接受数据格式
                data: {mac:macadress},   //要传递的数据
                async: false,
                success: function (data) {
                    if(data.data){
                        window.parent.alertSuccess('执行唤醒成功！');
                    }else{
                        window.parent.alertError('执行唤醒失败！');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    window.parent.alertError('执行唤醒异常！');
                }
            });
        }


        //执行删除
        function execDelete(dataIdp){
            var urlStr = ctxPath + '/jm/manager/wakeup/execDelete';
            jQuery.ajax({
                url: urlStr,
                type: "POST",
                dataType: 'json',//接受数据格式
                data: {dataId:dataIdp},   //要传递的数据
                async: false,
                success: function (data) {
                    if(data.data){
                        window.parent.alertSuccess('执行删除成功！');
                        window.location.reload();
                    }else{
                        window.parent.alertError('执行删除失败！');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    window.parent.alertError('执行删除异常！');
                }
            });
        }

    </script>
</body>
</html>
